package cate.game.demo;

import cate.game.GameBody;
import cate.game.demo.merge.DemoMergeLocal;
import cate.game.demo.rpc.DemoRpcLocal;
import cate.game.demo.spo.DemoStatus;
import cate.game.framework.ModuleMan;
import cp.solution.ops.status.ModOprStatus;

/** 演示模块 */
public class DemoManager extends ModuleMan {

    public DemoStatus status;

    public DemoMergeLocal merge = new DemoMergeLocal();
    public DemoRpcLocal rpc = new DemoRpcLocal();

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        merge.initIndependent(game);
        rpc.initIndependent(game);
    }

    @Override
    public void initDependent() throws Exception {
        status = merge.loadStatus(game);
    }

    public void tick() {
        try {
            rpc.tick();
        } catch (Exception e) {
            log.error("", e);
        }
    }

    @Override
    public void shutdown() {
        log.info("demo管理器安全关闭");
        saveStatus();
    }

    public void saveStatus() {
        game.db.kv.save(status);
    }

    @Override
    public ModOprStatus handleMergeSettle() {
        ModOprStatus item = new ModOprStatus("demo");
        return item.exe(() -> {
            log.info("demo::handleMergeSettle running");
            item.setLogs("完美结算(仅供演示)");
        });
    }

    @Override
    public ModOprStatus handleCleanData() {
        ModOprStatus item = new ModOprStatus("demo");
        return item.exe(() -> {
            log.info("demo::handleCleanData running");
            item.setLogs("完美清理(仅供演示)");
        });
    }
}
